<?php
Yii::import('application.components.login.dictograph.*');
class LoginPlus{

	public static $_self;
	protected $_dictograph;
	
	protected $_auth;
	
	public static function Login(LoginAuth $auth){
		if (!self::$_self instanceof self) {
			self::$_self = new self();
			self::$_self->dictographBuild();
		}
		self::$_self->_auth = $auth;
		self::$_self->loginProcess();
		return self::$_self;
	}
	
	/**
	 * 登录资格验证(反欺诈，白名单，测试者IP)
	 */
	public function loginProcess(){
	    
		if($this->_auth->authenticate()) {
		    //白名单 
		    if($this->_auth instanceof SysAuth && $this->_auth->status!='register'){
		        Yii::app()->user->login($this->_auth->identity());
		    }
		    //测试IP脚本运行后可跳过反欺诈验证 test/testip
		    //取消测试IP test/deltestip
		    if(Yii::app()->cache->get('test_ip')==$_SERVER["REMOTE_ADDR"]){
		        Yii::app()->user->login($this->_auth->identity());
		    }
		    //反欺诈验证
            /* $frozen=new Frozen();
            $frozen->ip=$_SERVER["REMOTE_ADDR"];
            $frozen->customer_id=$this->_auth->primary; */
            
           // if($frozen->checkAll()){
                //验证egunner用户状态
            $email_confirm=$this->check_email(array('username'=>$_POST['FormAuth']['user_name']));
            if($email_confirm){
                Yii::app()->user->login($this->_auth->identity());
            }else{
                //跳入确认邮件页面
                Yii::app()->session['mail_user']=$_POST['FormAuth']['user_name'];
                Yii::app()->getRequest()->redirect(Yii::app()->params['site_url']."/register/sendmail");
            }
                
            //}else{
            // Yii::app()->getRequest()->redirect(Yii::app()->baseUrl);
            //}
		}
	}
	/**
	 * eGUNNER用户状态检验 通过CURL发送取得返回值
	 * @param array $data
	 */
	public function check_email($data){
	    $return=$this->sendCurl($data, Yii::app()->params['egunner']."check_email.php");
	    if(!empty($return)){
	        $return_arr=json_decode($return);
	        if($return_arr->result==true){
	            return true;
	        }
	        else{
	            return false;
	        }
	    }else{
	        return false;
	    }
	}
	public function sendCurl($data,$url){
	    $ch = curl_init($url);
	    curl_setopt($ch, CURLOPT_POST, 1);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($ch, CURLOPT_HEADER, 0 );
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    $output = curl_exec($ch);
	    if (curl_errno($ch)) {
	        echo 'Errno'.curl_error($ch);
	    }
	    curl_close($ch);
	    return $output;
	}
	protected function dictographBuild(){
		$dictograph = $this->dictograph();
		foreach ($dictograph as $name => $class){
			$this->_dictograph[$name] = new $class();
		}
	}
	
	public function dictograph(){
		return array();
	}
	
	
}